<?php
include_once("../../untils/conn.php");

$search_field = isset($_GET['search_field']) ? $_GET['search_field'] : '';
$keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';

if ($con) {
  if ($db) {
    // 根据不同的查询类型构建 SQL 查询语句
    switch ($search_field) {
      case 'channel':
        $sql = "SELECT * FROM oder WHERE channel = '$keyword' ORDER BY id DESC";
        break;
      case 'channel_one':
        $sql = "SELECT * FROM oder WHERE channel_one = '$keyword' ORDER BY id DESC";
        break;
      case 'channel_two':
        $sql = "SELECT * FROM oder WHERE channel_two = '$keyword' ORDER BY id DESC";
        break;
      default:
        $sql = "SELECT * FROM oder ORDER BY id DESC";
        break;
    }

    // 获取分页数据
    $data = mysqli_query($con, $sql);
    $maxrows = mysqli_num_rows($data);
    $page_size = 10;
    if ($maxrows % $page_size == 0) {
      $maxpage = (int)($maxrows / $page_size);
    } else {
      $maxpage = (int)($maxrows / $page_size) + 1;
    }
    if (isset($_GET['curpage'])) {
      $page = $_GET['curpage'];
    } else {
      $page = 1;
    }
    $start = $page_size * ($page - 1);
    $get_sql = $sql . " LIMIT $start,$page_size";
    $data = mysqli_query($con, $get_sql);
?>

    <!-- 在这里显示查询到的数据 -->
    <table>
      <thead>
        <tr>
          <th>订单ID</th>
          <th>订单来源</th>
          <th>商品名称</th>
          <th>姓名</th>
          <th>手机号码</th>
        </tr>
      </thead>
      <tbody>
        <?php while ($row = mysqli_fetch_array($data)) { ?>
          <tr>
            <td><?php echo $row['id']; ?></td>
            <td><?php echo $row['channel']; ?></td>
            <td><?php echo $row['goods']; ?></td>
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['phone']; ?></td>
          </tr>
        <?php } ?>
      </tbody>
    </table>

    <!-- 分页代码 -->
    <div class="pagination">
      <?php
      echo "共 $maxpage 页&nbsp;&nbsp;";
      echo "每页 $page_size 项&nbsp;&nbsp;";
      if ($page > 1) {
        $prepage = $page - 1;
        echo "<a href='?curpage=$prepage'>上一页</a>&nbsp;&nbsp;";
      }
      if ($page < $maxpage) {
        $nextpage = $page + 1;
        echo "<a href='?curpage=$nextpage'>下一页</a>&nbsp;&nbsp;";
      }
      echo "&nbsp;&nbsp;第 $page 页</p>";
      ?>
    </div>

<?php
  }
}

// 安全过滤
function safe_filter($filter)
{
  $filter = str_replace("'", "''", $filter);
  $filter = str_replace("%", "\%", $filter);
  $filter = str_replace("_", "\_", $filter);
  $filter = str_replace("=", "", $filter);
  $filter = str_replace(" ", "", $filter);
  $filter = str_replace("\,", ",", $filter);
  $filter = str_replace("\\", "\\\\", $filter);
  return $filter;
}

$search_field = safe_filter($search_field);
$keyword = safe_filter($keyword);

?>